/*
 * Albareth - an action roleplaying game.
 * Copyright (C) 2008 Jens Anuth
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *   02110-1301, USA.
 * 
 * $Id: ParticleGameEffect.java 45 2008-05-14 23:04:29Z jens464 $
*/
package albareth.frontend.slick;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.particles.ConfigurableEmitter;
import org.newdawn.slick.particles.ParticleSystem;
import org.newdawn.slick.particles.ConfigurableEmitter.SimpleValue;

import frosch.slick.particle.FastDuplicatableEmitter;

import albareth.core.GameEffect;

public abstract class ParticleGameEffect extends AbstractSlickGameEffect
{
    protected ParticleSystem system = null;
    private float _rotation = 0;
    
    
    public ParticleGameEffect(GameEffect effectConfig) throws SlickException
    {
        super(effectConfig);
        ParticleSystem sys = SlickResources.getParticleSystem(
                config.getEffectId()
        );
        this.system = FastDuplicatableEmitter.duplicateSystem(sys);
    }
    
    public void renderEffect(Graphics g, int x, int y) throws SlickException
    {
        system.render(x, y);
    }
    
    public void update(int delta) throws SlickException
    {
        system.update(delta);
        super.update(delta);
    }

    public float getRotation()
    {
        return _rotation;
    }

    public void setRotation(float rotation)
    {
        for (int i = 0; i < system.getEmitterCount(); i++)
        {
            ConfigurableEmitter emitter = (ConfigurableEmitter)system.getEmitter(i);
            
            if (emitter == null) {
                continue;
            }
            SimpleValue angularOffset = (SimpleValue)emitter.angularOffset;
            angularOffset.setValue(rotation);
            _rotation = angularOffset.getValue(0);
        }

    }

    protected boolean hasFinished()
    {
        return system.getParticleCount() == 0;
    }
    
}
